package my.peerencrypt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import my.peerencrypt.User;
import my.peerencrypt.utils.DataSourceManager;
import my.peerencrypt.utils.JdbcUtils;

public class UserDao {

	DataSource ds = DataSourceManager.getInstance().getDataSource("testdb");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		UserDao userDao = new UserDao();
		List<User> list = userDao.getAllUser();
		for(User u:list){
			System.out.println(u.getUserName());
		}
		User u = userDao.getUserByUserName("XM001");
		System.out.println(u.getUserName());
	}

	public List<User> getAllUser() {
		List<User> list = new ArrayList<User>();
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			conn = ds.getConnection();
			String sql = "select * from  users";
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String userName = rs.getString("username");
				String password = rs.getString("password");
				String registrandom = rs.getString("registrandom");
				String key = rs.getString("registkey");
				int regiested = rs.getInt("registed");

				User u = new User();
				u.setUserName(userName);
				u.setPassword(password);
				u.setRegistrandom(registrandom);
				u.setKey(key);
				u.setHasRegister(regiested != 0);

				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(rs);
			JdbcUtils.release(st);
			JdbcUtils.release(conn);
		}
		return list;
	}

	public void registUser(User u){
		String sql = "update users set registed=1,registrandom=?,registkey=? where username=?";
		PreparedStatement pst = null;
		Connection conn = null;
		try {
			conn = ds.getConnection();
			pst = conn.prepareStatement(sql);
			pst.setString(1, u.getRegistrandom());
			pst.setString(2, u.getKey());
			pst.setString(3, u.getUserName());
			pst.execute();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(pst);
			JdbcUtils.release(conn);
		}
	}
	
	public User getUserByUserName(String userName) {
		if(userName==null || "".equals(userName)){
			return null;
		}
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		User u = null;
		try {
			conn = ds.getConnection();
			String sql = "select * from  users where username=?";
			pst = conn.prepareStatement(sql);
			pst.setString(1, userName);
			rs = pst.executeQuery();
			if (rs.next()) {
				String password = rs.getString("password");
				String registrandom = rs.getString("registrandom");
				String key = rs.getString("registkey");
				int regiested = rs.getInt("registed");

				u = new User();
				u.setUserName(userName);
				u.setPassword(password);
				u.setRegistrandom(registrandom);
				u.setKey(key);
				u.setHasRegister(regiested != 0);

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(rs);
			JdbcUtils.release(pst);
			JdbcUtils.release(conn);
		}
		return u;
	}
}
